﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes.effects {
    import Engine.Classes.*;
    import Engine.Events.*;
    import flash.display.*;
    import flash.geom.*;
    import flash.utils.*;
    import Classes.*;
    import flash.filters.*;
    import flash.text.*;
    import com.zynga.skelly.util.color.*;

    public class SparkleEffect extends MapResourceEffect {

        private static var effectName:String = "sparkle";
        private static var itemImage:ItemImage;
        private static var owners:Dictionary = new Dictionary(true);
        private static var zeroPt:Point = new Point();

        private var isActive:Boolean = true;
        private var bmask:BitmapData;
        private var contentBitmap:Bitmap;
        private var lastContentBitmap:Bitmap;
        private var sourceBitmapData:BitmapData;
        private var contentBitmapData:BitmapData;
        private var position:Number = -500;
        private var velocity:Number = 0;
        private var startVelocity:Number = 20;
        private var gravity:Number = -2;
        private var itemImage:ItemImage;
        private var effectImage:DisplayObject;
        private var effectPoint:Point;
        private var lastAnimatedFrame:Number = 0;
        private var _effectText:String = "";

        public function SparkleEffect(_arg1:MapResource, _arg2:String=""){
            this._effectText = _arg2;
            super(_arg1);
        }
        override public function animate(_arg1:uint):Boolean{
            var _local4:Number;
            var _local5:AnimatedBitmap;
            var _local6:int;
            var _local7:uint;
            var _local8:BitmapData;
            var _local9:CompositeItemImage;
            var _local10:DisplayObject;
            var _local11:int;
            var _local12:int;
            var _local13:uint;
            var _local14:ColorMatrix;
            var _local15:TextField;
            var _local16:TextFormat;
            if (!this.isActive){
                return (this.isActive);
            };
            var _local2:DisplayObject = m_mapResource.content;
            var _local3:Boolean = Boolean((((_local2 is Bitmap)) || ((_local2 is CompositeItemImage))));
            if (_local3){
                if ((_local2 is Bitmap)){
                    this.contentBitmap = (_local2 as Bitmap);
                    this.contentBitmapData = this.contentBitmap.bitmapData;
                    _local9 = new CompositeItemImage();
                    _local9.addChild(_local2);
                    m_mapResource.replaceContent(_local9);
                } else {
                    if ((_local2 is CompositeItemImage)){
                        _local10 = (_local2 as CompositeItemImage).getBuildingImageByType(ItemImageInstance.BUILDING_IMAGE);
                        this.contentBitmap = (_local10 as Bitmap);
                        this.contentBitmapData = this.contentBitmap.bitmapData;
                    };
                };
                if (((this.lastContentBitmap) && (!((this.lastContentBitmap.bitmapData == this.contentBitmap.bitmapData))))){
                    this.bmask = null;
                    if (this.sourceBitmapData){
                        this.sourceBitmapData.dispose();
                        this.sourceBitmapData = null;
                    };
                };
                this.lastContentBitmap = this.contentBitmap;
                if (!this.bmask){
                    this.effectPoint = new Point();
                    this.effectPoint.x = int((this.contentBitmapData.width * 0.66));
                    _local11 = 0;
                    _local12 = this.contentBitmapData.height;
                    while (_local11 < _local12) {
                        var _temp1 = _local11;
                        _local11 = (_local11 + 1);
                        _local13 = this.contentBitmapData.getPixel32(this.effectPoint.x, _temp1);
                        if (_local13){
                            this.effectPoint.y = _local11;
                            break;
                        };
                    };
                    this.loadAnimationEffect();
                    this.sourceBitmapData = this.contentBitmapData.clone();
                    if (!owners[this.contentBitmapData]){
                        this.bmask = this.sourceBitmapData.clone();
                        _local14 = new ColorMatrix();
                        _local14.adjustBrightness(30, 30, 30);
                        _local14.adjustSaturation(1);
                        _local14.adjustContrast(0.5, 0.5, 0.5);
                        this.bmask.applyFilter(this.bmask, this.bmask.rect, zeroPt, _local14.filter);
                        this.bmask.threshold(this.bmask, this.bmask.rect, zeroPt, "<=", 4286611584, 0, 4294967295);
                        if (this._effectText){
                            _local15 = new TextField();
                            _local15.textColor = 2284834;
                            _local15.thickness = 1;
                            _local15.text = this._effectText;
                            _local15.alpha = 0.33;
                            _local16 = new TextFormat("Arial", 24);
                            _local16.bold = true;
                            _local15.setTextFormat(_local16);
                            _local15.filters = [new GlowFilter(0x3300, 1, 3, 3, 3, 2)];
                            this.bmask.draw(_local15, new Matrix(1, 0, 0, 1, ((this.sourceBitmapData.width / 2) - 10), ((this.sourceBitmapData.height / 2) - 15)), null, null, null, true);
                        };
                        owners[this.contentBitmapData] = this.bmask;
                    } else {
                        this.bmask = (owners[this.contentBitmapData] as BitmapData);
                    };
                    this.contentBitmap.bitmapData = this.bmask.clone();
                    this.contentBitmapData = this.contentBitmap.bitmapData;
                };
                if (((!(this.effectImage)) || (!(this.itemImage)))){
                    return (true);
                };
                this.velocity = (this.velocity + this.gravity);
                this.position = (this.position + this.velocity);
                _local4 = (this.position * 0.01);
                _local5 = (this.effectImage as AnimatedBitmap);
                if (this.position < 0){
                    _local4 = 0;
                    if (this.position < -1000){
                        this.position = 0;
                        this.velocity = this.startVelocity;
                    };
                } else {
                    if (this.position > 100){
                        _local4 = 1;
                    } else {
                        if ((((((((this.position < 80)) && ((this.velocity < 0)))) && (_local5))) && (!(_local5.isPlaying)))){
                            this.lastAnimatedFrame = -1;
                            _local5.reset();
                        };
                    };
                };
                if (((_local5) && (_local5.isPlaying))){
                    if (_local5.currentFrame < this.lastAnimatedFrame){
                        _local5.currentFrame = 0;
                        _local5.stop();
                        this.lastAnimatedFrame = -1;
                    } else {
                        this.lastAnimatedFrame = _local5.currentFrame;
                    };
                };
                _local6 = (0xFF * _local4);
                _local7 = (_local6 << 24);
                _local8 = new BitmapData(this.sourceBitmapData.width, this.sourceBitmapData.height, true, _local7);
                this.contentBitmapData.copyPixels(this.sourceBitmapData, this.sourceBitmapData.rect, zeroPt, this.sourceBitmapData, zeroPt, false);
                this.contentBitmapData.copyPixels(this.bmask, this.bmask.rect, zeroPt, _local8, null, true);
                m_mapResource.renderBufferDirty = true;
            };
            if (!this.isActive){
                this.cleanUp();
            };
            return (this.isActive);
        }
        override public function cleanUp():void{
            if (this.contentBitmap){
                this.contentBitmap.bitmapData = this.sourceBitmapData.clone();
            };
            if (((this.effectImage) && (this.effectImage.parent))){
                this.effectImage.parent.removeChild(this.effectImage);
            };
            if (this.sourceBitmapData){
                this.sourceBitmapData.dispose();
                this.sourceBitmapData = null;
            };
            this.contentBitmapData = null;
            this.bmask = null;
            this.isActive = false;
        }
        private function loadAnimationEffect():void{
            var _local1:XML;
            var _local2:XML;
            if (!this.itemImage){
                _local1 = Global.gameSettings.getEffectByName(effectName);
                _local2 = _local1.image[0];
                this.itemImage = new ItemImage(_local2);
                this.itemImage.addEventListener(LoaderEvent.LOADED, this.onItemImageLoaded, false, 0, true);
                this.itemImage.load();
            } else {
                this.processItemImage();
            };
        }
        private function processItemImage():void{
            var _local1:ItemImageInstance;
            var _local2:AnimatedBitmap;
            var _local3:CompositeItemImage;
            if (!this.itemImage){
                this.loadAnimationEffect();
            };
            if ((m_mapResource.content is CompositeItemImage)){
                _local1 = this.itemImage.getInstance();
                if (!_local1){
                    return;
                };
                this.effectImage = (this.itemImage.getInstance().image as DisplayObject);
                if (!this.effectImage){
                    return;
                };
                this.effectImage.x = (this.effectPoint.x - (this.effectImage.width >> 1));
                this.effectImage.y = (this.effectPoint.y - (this.effectImage.height >> 1));
                _local2 = (this.effectImage as AnimatedBitmap);
                if (_local2){
                    _local2.stop();
                };
                _local3 = (m_mapResource.content as CompositeItemImage);
                _local3.addChild(this.effectImage);
            } else {
                this.isActive = false;
            };
        }
        private function onItemImageLoaded(_arg1:LoaderEvent):void{
            if (_arg1.target == this.itemImage){
                this.itemImage.removeEventListener(LoaderEvent.LOADED, this.onItemImageLoaded);
                this.processItemImage();
            };
        }

    }
}//package Classes.effects 
